package com.og.managedbean;

import java.io.Serializable;

import javax.enterprise.context.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;

import com.og.backingbean.Type;
import com.og.dao.CRUDEntityDAO;

/**
 * @author Renaud DIANA
 *
 */
@Named
@SessionScoped
public class TypeBean implements Serializable {

	private static final long serialVersionUID = 1L;

	/* Dependency injection */
	// Model
    @Inject
    private CRUDEntityDAO<Type> typeDAO;    
    // BackingBean
    @Inject
    private Type type;    
    @Inject
    private ListBean list;
    
    /* -------------------- */
    
	/**
	 * Default constructor
	 */
	public TypeBean() {
		// TODO Auto-generated constructor stub
	}

	public String create() {
				
		// Call model
		typeDAO.createEntity(type);
		
		// Generate new list
		list.findAllType();
		
		// Clear form
		type = new Type();
					
		// Add message
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Create type Successful"));  
		
		// Navigation case
		return "";
		//return "success";
	}
	
	public String update() {
		
		// Call model				
		typeDAO.updateEntity(type);
		
		// Generate new list
		list.findAllCompany();
				
		// Clear form
		type = new Type();
					
		// Add message
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Update type Successful"));  
		
		// Navigation case
		return "";
		//return "success";
	}
	
	public String delete() {
				
		// Call model				
		typeDAO.deleteEntity(type);
		
		// Generate new list
		list.findAllCompany();
				
		// Clear form
		type = new Type();
					
		// Add message
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Delete type Successful"));  
		
		// Navigation case
		return "";
		//return "success";
	}
	
	public String findOne() {
		
		// Call model				
		type = typeDAO.findOneEntity(type, type.getIdType());
		
		// Populate person(s)
		//person = company.getListPersons().get(0);
						
		// Clear form
		
				
		// Add message
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Find type Successful"));  
		
		// Navigation case
		return "";
		//return "success";
	}
	
	// Getters and Setters

	public Type getType() {
		return type;
	}

	public void setType(Type type) {
		this.type = type;
	}

}
